﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Tridion.ContentManager.CoreService.Client;
using TridionDesktopTools.Core;

namespace TridionDesktopTools.AdvancedSearch
{
    public partial class MainWindow
    {
        public TridionObjectInfo TridionFolder { get; set; }
        public TridionSelectorMode TridionSelectorMode { get; set; }
        public List<CreteriaRow> CreteriaRows { get; set; }
        public List<ItemFieldDefinitionData> Fields;

        public MainWindow()
        {
            InitializeComponent();

            this.cbBindingType.ItemsSource = Enum.GetNames(typeof(BindingType));
            this.cbBindingType.Text = Functions.GetFromIsolatedStorage("BindingType");
            if (String.IsNullOrEmpty(this.cbBindingType.Text))
                this.cbBindingType.SelectedIndex = 0;
            
            //get from isolated stoage
            this.txtHost.Text = Functions.GetFromIsolatedStorage("Host");
            this.txtUsername.Text = Functions.GetFromIsolatedStorage("Username");
            this.txtPassword.Password = Functions.GetFromIsolatedStorage("Password");

            this.TridionSelectorMode = TridionSelectorMode.Folder;

            this.CreteriaRows = new List<CreteriaRow>();
            CreteriaRow creteriaRow = new CreteriaRow();
            creteriaRow.cbField = this.cbField;
            creteriaRow.txtType = this.txtType;
            creteriaRow.cbOperation = this.cbOperation;
            creteriaRow.txtValue = this.txtValue;
            creteriaRow.dateValue = this.dateValue;
            creteriaRow.cbValue = this.cbValue;
            creteriaRow.btnRemove = this.btnRemove;
            creteriaRow.Index = 0;
            creteriaRow.Field = null;
            this.CreteriaRows.Add(creteriaRow);
        }

        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            Functions.ResetClient();

            Functions.ClientBindingType = (BindingType)Enum.Parse(typeof(BindingType), this.cbBindingType.Text);
            Functions.SaveToIsolatedStorage("BindingType", this.cbBindingType.Text);

            if (!Functions.EnsureValidClient(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password))
                return;

            this.lblTridionFolder.Visibility = Visibility.Visible;
            this.treeTridionFolder.Visibility = Visibility.Visible;
            this.lblSchema.Visibility = Visibility.Visible;
            this.cbSchema.Visibility = Visibility.Visible;

            this.TridionFolder = new TridionObjectInfo();
            
            string containerTcmId = Functions.GetFromIsolatedStorage(this.txtHost.Text + "_ContainerTcmId");
            if (!String.IsNullOrEmpty(containerTcmId))
            {
                this.TridionFolder.TcmId = containerTcmId;
            }

            string strContainerTcmIdPath = Functions.GetFromIsolatedStorage(this.txtHost.Text + "_ContainerTcmIdPath");
            if (!String.IsNullOrEmpty(strContainerTcmIdPath))
            {
                this.TridionFolder.TcmIdPath = strContainerTcmIdPath.Split(';').ToList();
            }

            List<ItemInfo> publications = Functions.GetPublications(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password).Expand(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password, TridionSelectorMode, this.TridionFolder.TcmIdPath, this.TridionFolder.TcmId).MakeExpandable();
            this.treeTridionFolder.ItemsSource = publications;

            if (publications != null && publications.Count > 0)
            {
                //save to isolated stoage
                Functions.SaveToIsolatedStorage("Host", this.txtHost.Text);
                Functions.SaveToIsolatedStorage("Username", this.txtUsername.Text);
                Functions.SaveToIsolatedStorage("Password", this.txtPassword.Password);
            }
        }

        private void TreeViewItem_Expanded(object sender, RoutedEventArgs e)
        {
            ItemInfo item = ((TreeViewItem)e.OriginalSource).DataContext as ItemInfo;
            if (item == null)
                return;

            Functions.OnItemExpanded(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password, item, this.TridionSelectorMode);
        }

        private void TreeViewItem_Selected(object sender, RoutedEventArgs e)
        {
            ItemInfo item = ((TreeViewItem)e.OriginalSource).DataContext as ItemInfo;
            if (item == null)
                return;

            this.TridionFolder.TcmId = item.TcmId;

            Functions.SaveToIsolatedStorage(this.txtHost.Text + "_ContainerTcmId", item.TcmId);

            List<ItemInfo> list = new List<ItemInfo>();
            Functions.AddPathItem(list, item);

            this.TridionFolder.TcmIdPath = list.Select(x => x.TcmId).ToList();
            
            Functions.SaveToIsolatedStorage(this.txtHost.Text + "_ContainerTcmIdPath", String.Join(";", this.TridionFolder.TcmIdPath));

            List<ItemInfo> schemas = Functions.GetSchemas(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password, Functions.GetPublicationTcmId(item.TcmId)).OrderBy(x => x.Title).ToList();

            list.Reverse();
            this.TridionFolder.NamedPath = string.Join("/", list.Select(x => x.Title));

            if (schemas.Count > 0)
            {
                this.cbSchema.ItemsSource = schemas;
                this.cbSchema.DisplayMemberPath = "Title";

                string schemaToSearchTcmId = Functions.GetFromIsolatedStorage(this.txtHost.Text + "_SchemaTcmId");
                if (!String.IsNullOrEmpty(schemaToSearchTcmId))
                {
                    this.cbSchema.SelectedIndex = schemas.FindIndex(x => x.TcmId == schemaToSearchTcmId);
                }
            }
        }

        private void treeTridionFolder_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ItemInfo item = this.treeTridionFolder.SelectedItem as ItemInfo;
            if (item == null)
                return;

            Process.Start(string.Format("http://{0}/SDL/#app=wcm&entry=cme&url=%23locationId%3Dtcm%3A{1}", this.txtHost.Text, item.TcmId.Replace("tcm:", "")));
        }

        private void cbSchema_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ItemInfo schema = this.cbSchema.SelectedValue as ItemInfo;
            if (schema == null)
                return;

            Functions.SaveToIsolatedStorage(this.txtHost.Text + "_SchemaTcmId", schema.TcmId);

            //load field list
            this.Fields = GetFields(schema.TcmId);

            foreach (CreteriaRow creteriaRow in this.CreteriaRows)
            {
                creteriaRow.cbField.ItemsSource = this.Fields;
                creteriaRow.cbField.DisplayMemberPath = "Name";

                string fieldName = Functions.GetFromIsolatedStorage(this.txtHost.Text + "_" + schema.TcmId.Replace("tcm:", "") + "_" + creteriaRow.Index + "_FieldName");
                if (!String.IsNullOrEmpty(fieldName))
                {
                    creteriaRow.cbField.SelectedIndex = this.Fields.FindIndex(x => x.Name == fieldName);
                }
            }

            this.grdCriteria.Visibility = Visibility.Visible;
            this.spButtons.Visibility = Visibility.Visible;
        }

        private void cbField_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CreteriaRow creteriaRow = this.CreteriaRows.FirstOrDefault(x => x.cbField == sender as ComboBox);
            if (creteriaRow == null)
                return;
            
            ItemInfo schema = this.cbSchema.SelectedValue as ItemInfo;
            if (schema == null)
                return;

            ItemFieldDefinitionData field = creteriaRow.cbField.SelectedValue as ItemFieldDefinitionData;
            if (field == null)
                return;

            Functions.SaveToIsolatedStorage(this.txtHost.Text + "_" + schema.TcmId.Replace("tcm:", "") + "_" + creteriaRow.Index + "_FieldName", field.Name);

            //load type name
            creteriaRow.txtType.Text = field.Name == "< Any >" ? "" : (field.GetFieldTypeName() + (field.IsMultiValue() ? " (MV)" : ""));

            //load operations
            List<Operation> operations = this.GetOperations(field);
            creteriaRow.cbOperation.ItemsSource = operations;

            string operationName = Functions.GetFromIsolatedStorage(this.txtHost.Text + "_" + field.Name.Replace("<", "").Replace(">", "").Replace(" ", "") + "_OperationName");
            if (!String.IsNullOrEmpty(operationName))
            {
                creteriaRow.cbOperation.SelectedIndex = operations.FindIndex(x => x.ToString() == operationName);
            }

            //bind combobox values
            this.BindComboValues(field, creteriaRow.cbValue);

            //load value
            creteriaRow.Value = Functions.GetFromIsolatedStorage(this.txtHost.Text + "_" + field.Name.Replace("<", "").Replace(">", "").Replace(" ", "") + "_Value");
            creteriaRow.Field = field;
        }

        private void cbOperation_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CreteriaRow creteriaRow = this.CreteriaRows.FirstOrDefault(x => x.cbOperation == sender as ComboBox);
            if (creteriaRow == null)
                return;

            ItemFieldDefinitionData field = creteriaRow.cbField.SelectedValue as ItemFieldDefinitionData;
            if (field == null)
                return;

            if (creteriaRow.cbOperation.SelectedValue == null)
                return;

            Operation operation = (Operation)creteriaRow.cbOperation.SelectedValue;

            Functions.SaveToIsolatedStorage(this.txtHost.Text + "_" + field.Name.Replace("<", "").Replace(">", "").Replace(" ", "") + "_OperationName", operation.ToString());
        }

        private void cbValue_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CreteriaRow creteriaRow = this.CreteriaRows.FirstOrDefault(x => x.cbValue == sender as ComboBox);
            if (creteriaRow == null)
                return;

            ItemFieldDefinitionData field = creteriaRow.cbField.SelectedValue as ItemFieldDefinitionData;
            if (field == null)
                return;

            //save value to isolated storage
            Functions.SaveToIsolatedStorage(this.txtHost.Text + "_" + field.Name.Replace("<", "").Replace(">", "").Replace(" ", "") + "_Value", creteriaRow.Value.ToString());
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            CreteriaRow creteriaRow = this.CreteriaRows.FirstOrDefault(x => x.btnRemove == sender as Button);
            if (creteriaRow == null)
                return;

            this.grdCriteria.Children.Remove(creteriaRow.cbField);
            this.grdCriteria.Children.Remove(creteriaRow.txtType);
            this.grdCriteria.Children.Remove(creteriaRow.cbOperation);
            this.grdCriteria.Children.Remove(creteriaRow.txtValue);
            this.grdCriteria.Children.Remove(creteriaRow.dateValue);
            this.grdCriteria.Children.Remove(creteriaRow.cbValue);
            this.grdCriteria.Children.Remove(creteriaRow.btnRemove);
            
            this.CreteriaRows.Remove(creteriaRow);

            int index = 0;
            foreach (CreteriaRow row in this.CreteriaRows)
            {
                row.Index = index;
                Grid.SetRow(row.cbField, index);
                Grid.SetRow(row.txtType, index);
                Grid.SetRow(row.cbOperation, index);
                Grid.SetRow(row.txtValue, index);
                Grid.SetRow(row.dateValue, index);
                Grid.SetRow(row.cbValue, index);
                Grid.SetRow(row.btnRemove, index);
                row.btnRemove.Visibility = this.CreteriaRows.Count > 1 ? Visibility.Visible : Visibility.Hidden;
                index++;
            }

            Grid.SetRow(this.btnAnd, index);
        }

        private void btnAnd_Click(object sender, RoutedEventArgs e)
        {
            ItemInfo schema = this.cbSchema.SelectedValue as ItemInfo;
            if (schema == null)
                return;

            CreteriaRow creteriaRow = new CreteriaRow();

            int index = Grid.GetRow(this.btnAnd);
            creteriaRow.Index = index;

            //<ComboBox Name="cbField" Width="160" Height="23" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0" Grid.Row="0" Margin="6" SelectionChanged="cbField_SelectionChanged" />
            ComboBox cbFieldNew = new ComboBox();
            cbFieldNew.Name = "cbField" + index;
            cbFieldNew.Width = 160;
            cbFieldNew.Height = 23;
            cbFieldNew.HorizontalAlignment = HorizontalAlignment.Left;
            cbFieldNew.VerticalAlignment = VerticalAlignment.Center;
            cbFieldNew.Margin = new Thickness(6);
            Grid.SetColumn(cbFieldNew, 0);
            Grid.SetRow(cbFieldNew, index);
            cbFieldNew.SelectionChanged += cbField_SelectionChanged;
            this.grdCriteria.Children.Add(cbFieldNew);

            cbFieldNew.ItemsSource = this.Fields;
            cbFieldNew.DisplayMemberPath = "Name";

            string fieldName = Functions.GetFromIsolatedStorage(this.txtHost.Text + "_" + schema.TcmId.Replace("tcm:", "") + "_" + index + "_FieldName");
            if (!String.IsNullOrEmpty(fieldName))
            {
                cbFieldNew.SelectedIndex = this.Fields.FindIndex(x => x.Name == fieldName);
            }

            ItemFieldDefinitionData field = cbFieldNew.SelectedValue as ItemFieldDefinitionData ?? new ItemFieldDefinitionData { Name = "< Any >" };

            creteriaRow.cbField = cbFieldNew;

            //<TextBlock Name="txtType" Width="120" Height="16" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="1" Grid.Row="0" />
            TextBlock txtTypeNew = new TextBlock();
            txtTypeNew.Name = "txtType" + index;
            txtTypeNew.Width = 120;
            txtTypeNew.Height = 16;
            txtTypeNew.HorizontalAlignment = HorizontalAlignment.Left;
            txtTypeNew.VerticalAlignment = VerticalAlignment.Center;
            Grid.SetColumn(txtTypeNew, 1);
            Grid.SetRow(txtTypeNew, index);
            this.grdCriteria.Children.Add(txtTypeNew);

            //load type name
            txtTypeNew.Text = field.Name == "< Any >" ? "" : (field.GetFieldTypeName() + (field.IsMultiValue() ? " (MV)" : ""));

            creteriaRow.txtType = txtTypeNew;

            //<ComboBox Name="cbOperation" Width="80" Height="23" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="2" Grid.Row="0" Margin="6" SelectionChanged="cbOperation_SelectionChanged" />
            ComboBox cbOperationNew = new ComboBox();
            cbOperationNew.Name = "cbOperation" + index;
            cbOperationNew.Width = 80;
            cbOperationNew.Height = 23;
            cbOperationNew.HorizontalAlignment = HorizontalAlignment.Left;
            cbOperationNew.VerticalAlignment = VerticalAlignment.Center;
            cbOperationNew.Margin = new Thickness(6);
            Grid.SetColumn(cbOperationNew, 2);
            Grid.SetRow(cbOperationNew, index);
            cbOperationNew.SelectionChanged += cbOperation_SelectionChanged;
            this.grdCriteria.Children.Add(cbOperationNew);

            //load operations
            List<Operation> operations = this.GetOperations(field);
            cbOperationNew.ItemsSource = operations;

            string operationName = Functions.GetFromIsolatedStorage(this.txtHost.Text + "_" + field.Name.Replace("<", "").Replace(">", "").Replace(" ", "") + "_OperationName");
            if (!String.IsNullOrEmpty(operationName))
            {
                cbOperationNew.SelectedIndex = operations.FindIndex(x => x.ToString() == operationName);
            }

            creteriaRow.cbOperation = cbOperationNew;

            //<TextBox Name="txtValue" Width="106" Height="23" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="3" Grid.Row="0" Margin="6" />
            TextBox txtValueNew = new TextBox();
            txtValueNew.Name = "txtValue" + index;
            txtValueNew.Width = 106;
            txtValueNew.Height = 23;
            txtValueNew.HorizontalAlignment = HorizontalAlignment.Left;
            txtValueNew.VerticalAlignment = VerticalAlignment.Center;
            txtValueNew.Margin = new Thickness(6);
            Grid.SetColumn(txtValueNew, 3);
            Grid.SetRow(txtValueNew, index);
            this.grdCriteria.Children.Add(txtValueNew);

            creteriaRow.txtValue = txtValueNew;

            //<DatePicker Name="dateValue" Width="106" Height="23" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="3" Grid.Row="0" Margin="6"/>
            DatePicker dateValueNew = new DatePicker();
            dateValueNew.Name = "dateValue" + index;
            dateValueNew.Width = 106;
            dateValueNew.Height = 23;
            dateValueNew.HorizontalAlignment = HorizontalAlignment.Left;
            dateValueNew.VerticalAlignment = VerticalAlignment.Center;
            dateValueNew.Margin = new Thickness(6);
            Grid.SetColumn(dateValueNew, 3);
            Grid.SetRow(dateValueNew, index);
            this.grdCriteria.Children.Add(dateValueNew);

            creteriaRow.dateValue = dateValueNew;

            //<ComboBox Name="cbValue" Width="106" Height="23" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="3" Grid.Row="0" Margin="6" SelectionChanged="cbValue_OnSelectionChanged" />
            ComboBox cbValueNew = new ComboBox();
            cbValueNew.Name = "cbValue" + index;
            cbValueNew.Width = 106;
            cbValueNew.Height = 23;
            cbValueNew.HorizontalAlignment = HorizontalAlignment.Left;
            cbValueNew.VerticalAlignment = VerticalAlignment.Center;
            cbValueNew.Margin = new Thickness(6);
            Grid.SetColumn(cbValueNew, 3);
            Grid.SetRow(cbValueNew, index);
            cbValueNew.SelectionChanged += cbValue_OnSelectionChanged;
            this.grdCriteria.Children.Add(cbValueNew);

            creteriaRow.cbValue = cbValueNew;

            //bind combobox values
            this.BindComboValues(field, creteriaRow.cbValue);

            //load value
            creteriaRow.Value = Functions.GetFromIsolatedStorage(this.txtHost.Text + "_" + field.Name.Replace("<", "").Replace(">", "").Replace(" ", "") + "_Value");

            //<Button Content="Remove" Name="btnRemove" Width="60" Height="23" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="4" Grid.Row="0" Margin="6" Click="btnRemove_Click" />
            Button btnRemoveNew = new Button();
            btnRemoveNew.Name = "btnRemove" + index;
            btnRemoveNew.Content = "Remove";
            btnRemoveNew.Width = 60;
            btnRemoveNew.Height = 23;
            btnRemoveNew.HorizontalAlignment = HorizontalAlignment.Left;
            btnRemoveNew.VerticalAlignment = VerticalAlignment.Center;
            btnRemoveNew.Margin = new Thickness(6);
            Grid.SetColumn(btnRemoveNew, 4);
            Grid.SetRow(btnRemoveNew, index);
            btnRemoveNew.Click += btnRemove_Click;
            this.grdCriteria.Children.Add(btnRemoveNew);

            creteriaRow.btnRemove = btnRemoveNew;

            creteriaRow.Field = field;

            //add row to collection
            this.CreteriaRows.Add(creteriaRow);

            if (index < 4)
            {
                Grid.SetRow(this.btnAnd, index + 1);
            }

            foreach (CreteriaRow row in this.CreteriaRows)
            {
                row.btnRemove.Visibility = this.CreteriaRows.Count > 1 ? Visibility.Visible : Visibility.Hidden;
            }
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            ItemInfo schema = cbSchema.SelectedValue as ItemInfo;
            if (schema == null)
                return;

            List<Criteria> creterias = new List<Criteria>();

            foreach (CreteriaRow creteriaRow in this.CreteriaRows)
            {
                ItemFieldDefinitionData field = creteriaRow.cbField.SelectedValue as ItemFieldDefinitionData;
                if (field == null)
                    continue;

                if (creteriaRow.cbOperation.SelectedValue == null)
                    continue;

                Operation operation = (Operation)creteriaRow.cbOperation.SelectedValue;

                Criteria criteria = new Criteria();
                criteria.Field = field;
                criteria.Operation = operation;
                criteria.Value = creteriaRow.Value;

                creterias.Add(criteria);

                //save values to isolated storage
                if (creteriaRow.Value != null)
                    Functions.SaveToIsolatedStorage(this.txtHost.Text + "_" + field.Name.Replace("<", "").Replace(">", "").Replace(" ", "") + "_Value", creteriaRow.Value.ToString());
            }

            //get results
            List<ItemInfo> res = this.GetComponentsByCriterias(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password, this.TridionFolder.TcmId, schema.TcmId, creterias);
            
            //show results
            ResultsWindow dialog = new ResultsWindow();
            dialog.ListBoxReport.ItemsSource = res.Select(itemInfo => new ResultInfo { TcmId = itemInfo.TcmId, ItemType = itemInfo.ItemType, Status = Status.Info, Message = string.Format("{0} ({1})", itemInfo.Title, itemInfo.TcmId) }).ToList();
            dialog.ListBoxReport.MouseDoubleClick += lbReport_OnMouseDoubleClick;
            dialog.Show();
        }

        private void lbReport_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            if (listBox == null)
                return;

            ResultInfo item = listBox.SelectedItem as ResultInfo;
            if (item == null)
                return;

            Process.Start(string.Format("http://{0}/WebUI/item.aspx?tcm=16#id={1}", this.txtHost.Text, item.TcmId));
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private List<ItemInfo> GetComponentsByCriterias(string host, string username, string password, string tcmFolder, string tcmSchema, List<Criteria> creterias)
        {
            List<List<ItemInfo>> res = new List<List<ItemInfo>>();
            
            List<Criteria> textCriterias = creterias.Where(x => x.Field.IsText() || x.Field.IsRichText() || x.Field.IsTextSelect() || x.Field.IsKeyword() || x.Field.IsEmbedded()).ToList();
            List<ItemInfo> resText = Functions.GetComponentsByTextCriterias(host, username, password, tcmFolder, string.Empty, tcmSchema, textCriterias);
            res.Add(resText);

            List<Criteria> deteNumCriterias = creterias.Where(x => x.Field.IsDate() || x.Field.IsNumber()).ToList();
            List<ItemInfo> resDateNum = Functions.GetComponentsByDateOrNumberCriterias(host, username, password, tcmSchema, deteNumCriterias);
            res.Add(resDateNum);


            List<Criteria> anyCriterias = creterias.Where(x => x.Field.Name == "< Any >").ToList();
            foreach (Criteria anyCriteria in anyCriterias)
            {
                List<ItemInfo> resAny = Functions.GetComponentsByTextCriterias(host, username, password, tcmFolder, anyCriteria.Value.ToString(), tcmSchema, null);
                res.Add(resAny);
            }

            return Functions.Intersect(res.ToArray(), false);
        }

        private List<ItemFieldDefinitionData> GetFields(string schemaTcmId)
        {
            List<ItemFieldDefinitionData> fields = Functions.GetSchemaFields(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password, schemaTcmId) ?? new List<ItemFieldDefinitionData>();
            List<ItemFieldDefinitionData> metadataFields = Functions.GetSchemaMetadataFields(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password, schemaTcmId);
            if (metadataFields != null && metadataFields.Count > 0)
            {
                foreach (ItemFieldDefinitionData metadataField in metadataFields)
                {
                    metadataField.Name = metadataField.Name + " [meta]";
                }
                fields.AddRange(metadataFields);
            }

            fields.Insert(0, new ItemFieldDefinitionData { Name = "< Any >" });
            return fields;
        }

        private List<Operation> GetOperations(ItemFieldDefinitionData field)
        {
            if (field.IsRichText() || field.IsEmbedded() || field.Name == "< Any >")
            {
                return new List<Operation> { Operation.Like };
            }
            if (field.IsText())
            {
                return new List<Operation> { Operation.Equal, Operation.Like };
            }
            if (field.IsDate() || field.IsNumber())
            {
                return new List<Operation> { Operation.Equal, Operation.Greater, Operation.Less };
            }
            return new List<Operation> { Operation.Equal };
        }

        private void BindComboValues(ItemFieldDefinitionData field, ComboBox cbValueCurrent)
        {
            if (field.IsKeyword())
            {
                KeywordFieldDefinitionData keywordField = (KeywordFieldDefinitionData)field;
                List<ItemInfo> keywords = Functions.GetKeywordsByCategory(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password, keywordField.Category.IdRef);
                cbValueCurrent.ItemsSource = keywords;
                cbValueCurrent.DisplayMemberPath = "Title";
            }
            if (field.IsTextSelect())
            {
                SingleLineTextFieldDefinitionData textField = (SingleLineTextFieldDefinitionData)field;
                cbValueCurrent.ItemsSource = textField.List.Entries;
            }
        }

    }
}